﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tracentsure
{
    public partial class FormMain : Form
    {
        InitFile init = new InitFile();
        HttpUitls http = new HttpUitls();
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            string key = InitFile.appKey;
            string secret = InitFile.appSecret;
            txtappKey.Text = key;
            txtappSecret.Text = secret;
        }

        private void btnGetToken_Click(object sender, EventArgs e)
        {
            try
            {
                string key = InitFile.appKey;
                string secret = InitFile.appSecret;
                string granttype = "client_credentials";
                List<HttpUitls.FormItemModel> formItems = new List<HttpUitls.FormItemModel>();
                formItems.Add(new HttpUitls.FormItemModel() { Key = "client_id", Value = key });
                formItems.Add(new HttpUitls.FormItemModel() { Key = "client_secret", Value = secret });
                formItems.Add(new HttpUitls.FormItemModel() { Key = "grant_type", Value = granttype });
                string url = InitFile.url + "/oauth/token";
                string result = init.FromUnicodeString(HttpUitls.PostForm(url, formItems));
                txtTokenJson.Text = result;
                JObject jObject = (JObject)JsonConvert.DeserializeObject(result);
                txtToken.Text = jObject["access_token"].ToString();
                txtType.Text = jObject["token_type"].ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string key = txtappSecret.Text;
            string id = System.Guid.NewGuid().ToString("N");
            string s = txtSource.Text;
            txtuuid.Text = id;
            txtBuild.Text = Base64Helper.AesEncrypt(key, s);
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string Authorization = txtType.Text + " " + txtToken.Text;
                

                model model = new model()
                {
                    uuid = txtuuid.Text,
                    appKey = txtappKey.Text,
                    content = txtBuild.Text
                };
                string Para = JsonConvert.SerializeObject(model);
                List<HttpUitls.FormItemModel> formItems = new List<HttpUitls.FormItemModel>();
                formItems.Add(new HttpUitls.FormItemModel() { Key = "Authorization", Value = Authorization });
                string url = InitFile.url + "/open-api/codes/order-trace-code";
                string result = init.FromUnicodeString(HttpUitls.sendPostData(url, formItems,Para));
                txtReturn.Text = result;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public class model
        {
            public string uuid {get;set;}
            public string appKey { get; set; }
            public string content { get; set; }

            
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            AppUitls uitls=new AppUitls();

            txtSource.Text = uitls.XS();
        }
    }
}

